cmake_minimum_required(VERSION 3.21.1)
project(signalBox VERSION 2.2.6 DESCRIPTION 信号工具箱 LANGUAGES C CXX)

find_package(Qt5 COMPONENTS Core Widgets Charts LinguistTools REQUIRED)
find_package(FLEX)
find_package(BISON)

# 设置可执行文件的名称
set(execName ${PROJECT_NAME})
# 设置使用的C++标准
set(CMAKE_CXX_STANDARD 11)
# 设置基本数据类型
set(BASIC_TYPE float)

# 将构建系统设置的基本数据类型向代码传递
add_definitions(-DBASIC_TYPE=${BASIC_TYPE})

# Release编译情况下, 添加WIN32标记以禁止控制台窗口
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
    message("build in debug")
    add_definitions(-DDEBUG=1)
else()
    message("build in resease")
    set(special_flag WIN32)
endif()

flex_target(SCANNER
    compiler/scanner.l
    ${CMAKE_CURRENT_BINARY_DIR}/scanner.cpp
    DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/scanner.h
    COMPILE_FLAGS "--noline"
)

bison_target(GRAMMAR
    compiler/grammar.ypp
    ${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp
    DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/grammar.h
    COMPILE_FLAGS "--debug --no-lines --token-table -d -Wcex"
)

add_flex_bison_dependency(SCANNER GRAMMAR)

include_directories(
    .
    ui
    calculator
    compiler
    util
    lib
    external_libs/z-fft
)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${Qt5Core_INCLUDE_DIRS})

set(TS_FILES ui/zh_CN.ts)
set(QRC_FILES ui/res.qrc)

set(PROJECT_SOURCES
    main.cpp
    ${FLEX_SCANNER_OUTPUTS}
    ${BISON_GRAMMAR_OUTPUTS}
    ui/mainWindow.ui
    ui/logo.rc
    ui/mainWindow.cpp
    ui/ChartView.cpp
    ui/SignalItem.cpp
    ui/SignalListWidget.cpp
    ui/SignalEditor.cpp
    ui/SignalEditorWidget.cpp
    ui/UIConfig.cpp
    ui/CodeTip.cpp
    ui/LoggerWindow.cpp
    ui/SignalSuffixManager.cpp
    compiler/symTable.cpp
    compiler/compiler.cpp
    compiler/ast.cpp
    compiler/libManager.cpp
    compiler/InnerLib.cpp
    calculator/calculator.cpp
    calculator/calculatorConf.cpp
    calculator/bufferAllocator.cpp
)

# 扫描工程生成和更新ts文件
# qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
# 发布ts文件, 也就是ts编译为qm
qt5_add_translation(QM_FILES ${TS_FILES})
# 添加资源文件
qt5_add_resources(PROJECT_SOURCES ${QRC_FILES})

if(ANDROID)
    add_library(${execName} SHARED
        ${PROJECT_SOURCES}
    )

    add_definitions(-DANDROID=1)
    add_definitions(-DANDROID_ABI="${ANDROID_ABI}")

    add_custom_command(TARGET ${execName}
        POST_BUILD
        WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
        COMMAND echo Deploy project to Android
        COMMAND androiddeployqt --input android_deployment_settings.json --output android-build/
    )
    add_custom_command(TARGET ${execName}
        POST_BUILD
        WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
        COMMAND cp android-build/build/outputs/apk/debug/android-build-debug.apk ${execName}.apk
    )
else()
    add_executable(${execName} ${special_flag}
        ${PROJECT_SOURCES}
    )
endif()

set_target_properties(${execName} PROPERTIES AUTOMOC TRUE AUTOUIC TRUE AUTORCC TRUE)

target_link_directories(${execName} PUBLIC lib)

target_link_libraries(${execName} ${Qt5Widgets_LIBRARIES} ${Qt5Charts_LIBRARIES})

file(GLOB libSrcFiles lib/*.cpp)

set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/lib)

add_library(libCommon SHARED lib/common/libCommon.cpp)

# 项目自带的动态库, 即内置函数库
foreach(srcFile ${libSrcFiles})
    cmake_path(RELATIVE_PATH srcFile)
    cmake_path(GET srcFile FILENAME fn)
    string(REPLACE "." ";" fn ${fn})
    list(GET fn 0 fn)
    add_library(${fn} SHARED
        ${srcFile}
    )
    target_link_directories(${fn} PUBLIC lib)
    target_link_libraries(${fn} libCommon ${Qt5Core_LIBRARIES} fft)
endforeach()

# 拷贝预置的FIR滤波器参数到目标目录
if(NOT EXISTS coefficient_demo)
message("Copy FIR coefficient")
file(COPY coefficient_demo DESTINATION .)
endif()

if(NOT EXISTS workspace_demo)
message("Copy workspace demo")
file(COPY workspace_demo DESTINATION .)
endif()

add_subdirectory(external_libs)
